package cn.hist.hfp.service;

import cn.hist.hfp.converter.CommodityConverter;
import cn.hist.hfp.entity.Commodity;
import cn.hist.hfp.exception.ResourceNotFoundException;
import cn.hist.hfp.repository.CommodityRepository;
import cn.hist.hfp.vo.CommodityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CommodityService {

    @Value(value = "${hfp.page-size.commodity}")
    private int pageSize;

    private final CommodityRepository commodityRepository;

    private final CommodityProducerService producerService;

    private final CommodityConverter commodityConverter;

    @Autowired
    public CommodityService(CommodityRepository commodityRepository, CommodityProducerService producerService, CommodityConverter commodityConverter) {
        this.commodityRepository = commodityRepository;
        this.producerService = producerService;
        this.commodityConverter = commodityConverter;
    }

    public CommodityVO create(Commodity commodity) {
        return commodityConverter.converter(commodityRepository.save(commodity));
    }

    public CommodityVO findOne(int id) {
        Optional<Commodity> optionalMallStuff = commodityRepository.findById(id);
        if (optionalMallStuff.isPresent()) {
            return commodityConverter.converter(optionalMallStuff.get());
        }
        throw new ResourceNotFoundException();
    }

    public List<CommodityVO> findSome(String type, int producer_id, int page) {
        if (type == null || "".equals(type)) {
            if (producer_id < 0)
                return commodityConverter.converter(commodityRepository.findAll(PageRequest.of(page, pageSize)).getContent());
            return commodityConverter.converter(commodityRepository.findByProducer(
                    producerService.findOne(producer_id), PageRequest.of(page, pageSize)).getContent());
        }
        if (producer_id < 0)
            return commodityConverter.converter(commodityRepository.findByType(type, PageRequest.of(page, pageSize)).getContent());
        return commodityConverter.converter(commodityRepository.findByTypeAndProducer(
                type, producerService.findOne(producer_id), PageRequest.of(page, pageSize)).getContent());
    }

    public int numberOfPages(String type, int producer_id) {
        if (type == null || "".equals(type)) {
            if (producer_id < 0)
                return commodityRepository.findAll(PageRequest.of(0, pageSize)).getTotalPages();
            return commodityRepository.findByProducer(
                    producerService.findOne(producer_id), PageRequest.of(0, pageSize)).getTotalPages();
        }
        if (producer_id < 0)
            return commodityRepository.findByType(type, PageRequest.of(0, pageSize)).getTotalPages();
        return commodityRepository.findByTypeAndProducer(
                type, producerService.findOne(producer_id), PageRequest.of(0, pageSize)).getTotalPages();
    }

    public CommodityVO update(Commodity commodity) {
        return commodityConverter.converter(commodityRepository.save(commodity));
    }

    public int delete(int id) {
        Optional<Commodity> optionalMallStuff = commodityRepository.findById(id);
        optionalMallStuff.ifPresent(commodityRepository::delete);
        return commodityRepository.findById(id).isPresent() ? 0 : 1;
    }
}
